package com.ruoyi.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseDto;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.JsonResponse;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.domain.dto.TCodeDto;
import com.ruoyi.system.domain.po.TCode;
import com.ruoyi.system.domain.po.TTag;
import com.ruoyi.system.domain.vo.TCodeVo;
import com.ruoyi.system.service.TCodeServier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/code")
@Api(tags = "变量、标签管理")
public class TCodeController {

    @Resource
    private TCodeServier tCodeServier;

    @ApiOperation("导入变量")
    @PostMapping("/importData")
    public JsonResponse importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TCodeDto.Import> util = new ExcelUtil<>(TCodeDto.Import.class);
        List<TCodeDto.Import> codeList = util.importExcel(file.getInputStream());
        tCodeServier.importCode(codeList);
        return JsonResponse.ok();
    }

    @ApiOperation("导出变量")
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, @RequestBody @Validated BaseDto<TCodeDto.Query> queryBaseDto) throws Exception
    {
        queryBaseDto.setPageNum(-1);
        List<TCodeVo.Result> results = tCodeServier.listCode(queryBaseDto.getParam());
        if (CollectionUtil.isNotEmpty(results)) {
            for (TCodeVo.Result result : results) {
                result.setTagName(result.getTagName());
            }
        }
        ExcelUtil<TCodeVo.Result> util = new ExcelUtil<TCodeVo.Result>(TCodeVo.Result.class);
        util.exportExcel(response, results, "变量列表");

    }

    @ApiOperation("查询变量")
    @PostMapping("/listCode")
    public JsonResponse<PageInfo<TCodeVo.Result>> listCode(@RequestBody @Validated BaseDto<TCodeDto.Query> list){
        if (list.getPageNum() > 0) {
            PageHelper.startPage(list.getPageNum(), list.getPageSize(), list.getSort());
        }
        return JsonResponse.ok(new PageInfo<>(tCodeServier.listCode(list.getParam())));
    }

    @ApiOperation("新增变量")
    @PostMapping("/insertCode")
    public JsonResponse insertCode(@RequestBody @Validated TCodeDto.Insert insert){
        return JsonResponse.ok(tCodeServier.insertCode(insert));
    }

    @ApiOperation("修改变量")
    @PostMapping("/updateCode")
    public JsonResponse updateCode(@RequestBody @Validated TCodeDto.Update update){
        return JsonResponse.ok(tCodeServier.updateCode(update));
    }

    @ApiOperation("删除变量")
    @PostMapping("/deleteCode")
    public JsonResponse deleteCode(@RequestBody @Validated TCodeDto.Delete delete){
        return JsonResponse.ok(tCodeServier.deleteCode(delete));
    }

    @ApiOperation("新增标签")
    @PostMapping("/insertTag")
    public JsonResponse insertTag(@RequestBody @Validated TCodeDto.TagInsert tagInsert){
        return JsonResponse.ok(tCodeServier.insertTag(tagInsert));
    }

    @ApiOperation("修改标签")
    @PostMapping("/updateTag")
    public JsonResponse updateTag(@RequestBody @Validated TCodeDto.TagUpdate tagUpdate){
        return JsonResponse.ok(tCodeServier.updateTag(tagUpdate));
    }

    @ApiOperation("删除标签")
    @PostMapping("/deleteTag")
    public JsonResponse deleteTag(@RequestBody @Validated TCodeDto.TagDelete tagDelete){
        return JsonResponse.ok(tCodeServier.deleteTag(tagDelete));
    }

    @ApiOperation("查询标签")
    @PostMapping("/listTag")
    public JsonResponse<List<TTag>> listTag(@RequestBody @Validated TCodeDto.QueryTag queryTag){
        return JsonResponse.ok(tCodeServier.listTag(queryTag));
    }
}
